package com.kanche.bigbaby.collection;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.kanche.bigbaby.collection.ui.ItemConfig;

import java.util.List;
import java.util.Map;

//import com.kkche.merchant.domain.ui.ItemConfig;

/**
 * @author: nealmi
 * Date: 9/27/13
 * Time: 5:12 PM
 */
public class ItemsAdapter extends BaseAdapter {
    private List<ItemConfig> mList;
    private Context mContext;

    public ItemsAdapter(List<ItemConfig> list, Context context) {
        mList = list;
        mContext = context;
    }


    public void updateValueForKey(String key, String value) {
        updateValueForKey(key, value, null, null);
    }

    public void updateValueForKey(String key, String value, String displayValue, Map<String, Object> extraInfo) {
        for (ItemConfig config : mList) {
            if (key.equals(config.getKey())) {
                config.clearErrorMessage();
                config.setValue(value);
                if (displayValue != null) {
                    config.setDisplayValue(displayValue);
                }
                if (extraInfo != null) {
                    config.setExtra(extraInfo);
                }
                break;
            }
        }
        notifyDataSetChanged();
    }


    public void refresh(List<ItemConfig> list) {
        mList = list;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ItemConfig item = (ItemConfig) getItem(position);
        return item.getView(convertView, mContext);
    }


    public List<ItemConfig> getData() {
        return mList;
    }

    public ItemConfig getItemForKey(String key) {
        for (ItemConfig config : mList) {
            if (config.getKey().equals(key)) {
                return config;
            }
        }
        return null;
    }
}